using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ruminate.GUI.Framework;
using Ruminate.Utils;

namespace Ruminate.GUI.Content {

    public class CheckBox : Element {

        /*####################################################################*/
        /*                               Variables                            */
        /*####################################################################*/

        /* ## Local Variables ## */            
        private IconRenderer _iconRenderer;
        private TextRenderer _textRenderer;
        private Texture2D _checked,  _checkedHover, _unchecked, _uncheckedHover;

        /* ## Exposed Variables ## */
        public ElementEvent OnToggle { get; set; }
        public ElementEvent OffToggle { get; set; }

        public string Label { get; set; }

        private bool _innerIsToggled;
        public bool IsToggled {
            get { return _innerIsToggled; }
            set {
                if (value != _innerIsToggled) {
                    if (value) {
                        if (OnToggle != null) { OnToggle(this); }
                    } else {
                        if (OffToggle != null) { OffToggle(this); }
                    }
                }

                _innerIsToggled = value;
            }
        }
        public void SetToggle(bool toggle) {
            _innerIsToggled = toggle;
        }

        /*####################################################################*/
        /*                           Initialization                           */
        /*####################################################################*/

        public CheckBox(int x, int y, string label) 
            : base(new Offset(x, y, 0, 0)) {

            Label = label;
            _innerIsToggled = false;
        }

        /*####################################################################*/
        /*                                Resized                             */
        /*####################################################################*/

        protected internal override void Init() {

            _iconRenderer = (IconRenderer)InstanceData.DefaultRenderers["CheckBox"];
            _textRenderer = (TextRenderer)InstanceData.DefaultRenderers["TextRenderer"];

            var size = _textRenderer.Font.MeasureString(Label);
            LocationComponent.Size = new Vector2(
                size.X + 2 + _iconRenderer.IconSize.X,
                _iconRenderer.IconSize.Y);
        }

        protected internal override void Render() {

            var size = LocationComponent.Size.ToPoint();

            _unchecked = _iconRenderer.Render(size, 0, _textRenderer, Label);
            _checked = _iconRenderer.Render(size, 1, _textRenderer, Label);

            _uncheckedHover = _iconRenderer.Render(size, 2, _textRenderer, Label);
            _checkedHover = _iconRenderer.Render(size, 3, _textRenderer, Label);
        }

        protected internal override Rectangle ChildRenderArea() {

            return LocationComponent.RenderBounds;
        }

        /*####################################################################*/
        /*                                Events                              */
        /*####################################################################*/

        /// <inheritdoc/>
        protected internal override void MouseClick(MouseEventArgs e) {

            IsToggled = !IsToggled;           
        }

        /*####################################################################*/
        /*                                Logic                               */
        /*####################################################################*/                

        protected internal override void Update() { }

        protected internal override void Draw() {

            if (IsToggled) {
                SpriteBatch.Draw(
                    IsHover ? _checkedHover : _checked,
                    LocationComponent.RenderLocation, 
                    Color.White);
            } else {
                SpriteBatch.Draw(
                    IsHover ? _uncheckedHover : _unchecked,
                    LocationComponent.RenderLocation,
                    Color.White);
            }
        }        
    }
}